<?php

namespace App\Http\Controllers\Backend;

use App\Http\Util\Common;
use App\Photo;
use App\Type;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class PhotosController extends BaseController
{
    public function __construct(Photo $model)
    {
        $this->model = $model;
        $this->view = 'photos';
        $this->title = 'Photos';
        $this->baseModel = Photo::class;
    }

    public function index()
    {
        $type = request()->get('id');
        $title = $this->title;
        return view('backend.'.$this->view.'.index',compact('title','type'));
    }

    public function validateUpdate($request)
    {
        if($request->hasFile('file')) {
            $file = $request->file('file');
            $data = [];

            $bool = Common::upload($file);


            if ($bool) {
                $data['image'] = $bool;
                return $data;
            }
            else {
                throw new \Exception('file upload fail');
            }
        }

        else {
            $data = $request->all();
            return $data;
        }


    }

    public function update(Request $request, $model)
    {
        $data = $this->validateUpdate($request);
        Storage::disk('public_uploads')->delete($model->image);
        $model->update($data);
        return redirect()->back();
    }

    public function destroy($model)
    {
        Storage::disk('public_uploads')->delete($model->image);
        $model->delete();
        return redirect()->back();
    }

    public function batchDelete()
    {
        $ids = request()->get('batch-delete');
        $ids = explode(',',$ids);
        foreach ($ids as $id) {
            $model = Photo::find($id);
            Storage::disk('public_uploads')->delete($model->image);
            $model->delete();
        }
        return redirect()->back();
    }

    public function slide($model)
    {
        $model->is_slide ? $model->is_slide = false : $model->is_slide = true;
        $model->save();
        return redirect()->back();
    }

    public function showUpload()
    {
        $title = 'Upload Photo';
        $types = Type::pluck('name', 'id')->toArray();
        $type_id = request()->get('id');
        if(isset($types[$type_id]))
            return view('backend.photos.upload',compact('title','types','type_id'));
        else return redirect()->to('/admin/');
    }

    public function showAll()
    {
        $data = request()->all();
        if(isset($data['id']))
            $photos = Photo::where('type_id',$data['id']);
        else $photos = Photo::where('is_slide',$data['slide']);
        $total = $photos->count();
        $photos = $photos->skip($data['offset'])->limit($data['limit'])->orderBy('weight','desc')->get();
        return response()->json([
            'status' => 0,
            'data' => [
                'data' => $photos,
                'total' => $total
            ]
        ]);
    }

    public function upload()
    {
        $file = request()->file('file');
        $type_id = request()->get('type_id');

        if ($file->isValid()) {

            $bool = Common::upload($file);

            if($bool) {
                $data['image'] = $bool;
                $data['type_id'] = $type_id;
                Photo::create($data);
                return response()->json([
                    'code' => 200,
                    'message' => 'success'
                ]);
            }
            else {
                return response()->json([
                    'code' => 500,
                    'message' => 'save fail'
                ],500);
            }
        }
        else {
            return response()->json([
                'code' => 500,
                'message' => 'file not exists'
            ],500);
        }
    }

}
